package initialize

import (
	"fmt"
	"go-bluebell/model"

	"gorm.io/gorm"

	"github.com/spf13/viper"

	"go.uber.org/zap"
	"gorm.io/driver/mysql"
)

func MySQLInit() (err error) {
	// "user:password@(localhost)/dbname?charset=utf8mb4&parseTime=True&loc=Local")

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		viper.GetString("mysql.username"),
		viper.GetString("mysql.password"),
		viper.GetString("mysql.host"),
		viper.GetInt("mysql.port"),
		viper.GetString("mysql.dbname"))
	model.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})

	if err != nil {
		zap.L().Error("MySQL连接失败！", zap.Error(err))
	}

	return
}
